/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latex ;

/**
 *
 * @author ersin
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ersin
 */
public class StateSave
{

    //private String filePath;
    private ArrayList pathList ;

    private String stateSaveFilePath ;

    public StateSave( )
    {
        this.pathList = new ArrayList( ) ;
        this.stateSaveFilePath = "state.dat" ;
    }

    public boolean addToPathList( String filePath )
    {
        try
        {
            boolean x = this.pathList.add( filePath ) ;
            return true ;
        }
        catch( Exception e )
        {
            return false ;
        }
    }

    public void clearPathList( )
    {
        try
        {
            this.pathList.clear( ) ;
        }
        catch( Exception e )
        {
            e.printStackTrace( ) ;
        }
    }

    public boolean removeFromPathList( String filePath )
    {
        try
        {
            int index = this.pathList.indexOf( filePath ) ;
            if( index != -1 )
            {
                this.pathList.remove( index ) ;
                return true ;
            }
            else
            {
                return false ;
            }
        }
        catch( Exception e )
        {
            return false ;
        }
    }

    public boolean stateSave( )
    {
        try
        {
            this.FileCreate( this.stateSaveFilePath ) ;
            //clearFile
            this.FileWrite( this.stateSaveFilePath, "", false ) ;

            for( int i = 0; i < this.pathList.size( ); i++ )
            {
                this.FileWrite( this.stateSaveFilePath, this.getFilePath( i ) + ",", true ) ;
            }
            return true ;
        }
        catch( Exception e )
        {
            return false ;
        }
    }

    public ArrayList ReturnPathlist( )
    {
        return this.pathList ;
    }

    public ArrayList ReturnFileNames( )
    {
        ArrayList fileNames = new ArrayList( ) ;
        String s = "" ;
        for( int i = 0; i < this.pathList.size( ); i++ )
        {
            s = String.valueOf( this.pathList.get( i ) ) ;
            fileNames.add( s.substring( s.lastIndexOf( "\\" ) + 1 ) ) ;
        }
        return fileNames ;
    }

    public boolean LoadPathsFromStateFile( )
    {
        try
        {
            File file = new File( this.stateSaveFilePath ) ;
            BufferedReader input = new BufferedReader( new FileReader( file ) ) ;
            try
            {
                String pathStrings = input.readLine( ) ;

                try
                {
                    int lastIndex = pathStrings.lastIndexOf( "," ) ;
                    pathStrings = pathStrings.substring( 0, lastIndex ) ;
                }
                catch( NullPointerException e )
                {
                    return false ;
                }

                String tempString = pathStrings ;
                int index = pathStrings.indexOf( "," ) ;
                if( index == -1 )
                {
                    this.addToPathList( pathStrings ) ;
                }
                while( index != -1 )
                {
                    tempString = pathStrings.substring( 0, index ) ;

                    this.addToPathList( tempString ) ;
                    pathStrings = pathStrings.substring( index + 1 ) ;

                    index = pathStrings.indexOf( "," ) ;
                    if( index == -1 )
                    {
                        this.addToPathList( pathStrings ) ;
                    }
                }
                return true ;
            }
            catch( IOException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace( ) ;
                return false ;
            }
        }
        catch( FileNotFoundException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace( ) ;
            return false ;
        }

    }

    public String getFilePath( int index )
    {
        int size = this.pathList.size( ) ;
        System.out.println( String.valueOf( size ) ) ;
        return ( String )this.pathList.get( index ) ;
    }

    private void FileCreate( String filePath ) throws IOException
    {
        File file = new File( filePath ) ;
        boolean success = file.createNewFile( ) ;
    }

    private void FileDelete( String filePath )
    {
        File file = new File( filePath ) ;
        file.deleteOnExit( ) ;
    }

    private void FileWrite( String filePath, String stringToWrite, boolean appendFlag ) throws IOException
    {
        FileWriter fstream = new FileWriter( filePath, appendFlag ) ;
        BufferedWriter out = new BufferedWriter( fstream ) ;
        out.write( stringToWrite ) ;
        //Close the output stream
        out.close( ) ;
    }
}
